import { defineStore } from "pinia";

const CACHE_EXPIRE_TIME = VITE_ENV.VITE_PAGE_CACHE_EXPIRE;

const usePageStore = defineStore("page", {
  state: () => {
    return {
      caches: [],
      stacks: []
    };
  },
  getters: {
    hasCachePage(state) {
      return name =>
        state.caches.findIndex(item => item.name == name) == -1 ? false : true;
    },
    getCachePages(state) {
      return state.caches.map(item => item.name);
    }
  },
  actions: {
    addCachePage(data) {
      this.caches.push(data);
    },
    removeExpireCache() {
      const time = new Date().getTime();
      this.caches = this.caches.filter(
        item => time - item.time < CACHE_EXPIRE_TIME
      );
    },
    clearCache() {
      this.caches = [];
    },
    pushStack(data) {
      this.stacks.push(data);
    },
    popStack() {
      return this.stacks.pop();
    }
  },
  persist: true
});
export default usePageStore;
